<script setup lang="ts">
import * as echarts from "echarts";
import { onMounted, ref } from "vue";

let charts = ref();

onMounted(() => {
  initCharts();
});

const initCharts = () => {
  let myChart = echarts.init(charts.value);
  myChart.setOption({
    title: {
      text: "年度游客量",
      show: false,
      textStyle: {
        color: "#fff",
        fontSize: 16,
        fontWeight: "normal",
      },
      left: "center",
      top: "20px",
    },
    xAxis: {
      type: "category",
      data: ["春季", "夏季", "秋季", "冬季"],
    },
    yAxis: {},
    legend: {
      // data的数据和series的name的数据一一对应
      data: ["2021年", "2020年", "2019年"],
      show: true,
      top: "20px",
      textStyle: {
        color: "#fff",
        fontSize: 12,
        fontWeight: "normal",
      },
    },
    tooltip: {
      trigger: "axis", //鼠标移入显示数据
    },
    series: [
      {
        type: "line",
        name: "2021年", //必须有name  不然legend不显示
        smooth: true,
        data: [100, 700, 300, 400],
        areaStyle: {
          color: {
            type: "linear",
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: "#7390cb", // 0% 处的颜色
              },
              {
                offset: 1,
                color: "blue", // 100% 处的颜色
              },
            ],
            global: false, // 缺省为 false
          },
        },
      },
      {
        type: "line",
        name: "2020年",
        smooth: true,
        data: [200, 500, 100, 300],
        areaStyle: {
          color: {
            type: "linear",
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: "#19b92e", // 0% 处的颜色
              },
              {
                offset: 1,
                color: "#24de3c", // 100% 处的颜色
              },
            ],
            global: false, // 缺省为 false
          },
        },
      },
      {
        type: "line",
        name: "2019年",
        smooth: true,
        data: [300, 400, 200, 200],
        areaStyle: {
          color: {
            type: "linear",
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: "#f7b500", // 0% 处的颜色
              },
              {
                offset: 1,
                color: "#f7d500", // 100% 处的颜色
              },
            ],
            global: false, // 缺省为 false
          },
        },
      },
    ],
    grid: {
      left: "10%",
      right: "10%",
      bottom: "10%",
      top: "20%",
    },
  });
};

defineOptions({
  name: "Year",
});
</script>

<template>
  <div class="box7">
    <div class="title">
      <p>年度游客量</p>
      <img src="../../images/dataScreen-title.png" alt="" />
    </div>
    <div class="charts" ref="charts"></div>
  </div>
</template>

<style scoped lang="scss">
.box7 {
  height: 100%;
  width: 100%;
  margin-top: 10px;
  background: url("../../images/dataScreen-main-cb.png") no-repeat;
  background-size: cover;

  .title {
    margin-left: 20px;
    margin-top: 10px;
    p {
      color: white;
      font-size: 16px;
    }
  }

  .charts {
    height: calc(100% - 40px);
    width: 100%;
    margin-top: 10px;
  }
}
</style>
